package com.weixingtu.commuterCar.dao.impl;

import com.weixingtu.commuterCar.bean.Device;
import com.weixingtu.commuterCar.dao.DeviceDao;
import com.weixingtu.commuterCar.util.DruidUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DeviceDaoImpl implements DeviceDao {
    @Override
    public void insert(Device device) {
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet result = null;
        String sql = "insert into device values(?,?,?);";
        try {
            state = conn.prepareStatement(sql);
            state.setInt(1,device.getDeviceId());
            state.setString(2,device.getImei());
            state.setInt(3,device.getLineId());
            state.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DruidUtil.close(conn, state, result);
        }
    }

    @Override
    public void update(Device device) {
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet result = null;

        String sql = "update device set lineId = ? where imei=?";

        try {
            state = conn.prepareStatement(sql);
            state.setInt(1,device.getLineId());
            state.setString(2,device.getImei());
            state.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DruidUtil.close(conn, state, result);
        }
    }

    @Override
    public void delete(String imei) {
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet result = null;
        String sql = "delete from device where imei=?";

        try {
            state = conn.prepareStatement(sql);
            state.setString(1,imei);
            state.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DruidUtil.close(conn, state, result);
        }
    }

    @Override
    public int queryByImei(String imei) {
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet result = null;
        String sql = "select * from device where imei=?";
        try {
            state = conn.prepareStatement(sql);
            state.setString(1,imei);
            result = state.executeQuery();
            if(result.next()){
                return result.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DruidUtil.close(conn, state, result);
        }
        return 0;
    }

    @Override
    public List<Device> queryAll(){
        List<Device> data = new ArrayList<>();
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet result = null;
        String sql = "select * from device order by lineId";
        try {
            state = conn.prepareStatement(sql);
            result = state.executeQuery();
            while (result.next()){
                Device temp = new Device();
                temp.setDeviceId(result.getInt("deviceId"));
                temp.setLineId(result.getInt("lineId"));
                temp.setImei(result.getString("imei"));
                data.add(temp);
            }
            return data;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DruidUtil.close(conn, state, result);
        }
        return null;
    }

}
